/**
 * 
 */
package any.desktop.gwt.client.ui;

import any.gwt.client.util.DispacherContainer;
import any.gwt.client.util.DispatchEvent;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * TODO: Doc me
 * 
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class SearchBox extends TextBox implements ClickListener {

    public SearchBox() {
        super();
        setStyleName("searchBox");
        addKeyboardListener(new KeyboardListenerAdapter() {

            public void onKeyPress(final Widget sender, final char keyCode,
                    final int modifiers) {
                if (keyCode == KEY_ENTER) {
                    doSearch();
                }
            }
        });
    }

    public void onClick(final Widget sender) {
        doSearch();

    }

    protected void doSearch() {
        final String queryString = getText().trim();
        History.newItem("/search?q=" + queryString + "&s=0");
        DispacherContainer.getGlobalDispatcher().dispatch(
                new DispatchEvent(this, queryString, "doSearch"));
    }
}
